import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/24 17:16
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public List<Integer> largestValues(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }
        List<Integer> ans = new ArrayList<>();
        dfs(ans, root, 0);
        return ans;
    }
    public void dfs(List<Integer> ans, TreeNode node, int curHeight) {
        if (curHeight == ans.size()) {
            // 当前到达最底层，那么需要加入，扩充深度
            ans.add(node.val);
        }else {
            ans.set(curHeight, Math.max(ans.get(curHeight), node.val));
        }
        if (node.left != null) {
            dfs(ans, node.left, curHeight + 1);
        }
        if (node.right != null) {
            dfs(ans, node.right, curHeight + 1);
        }
    }
}
